package com.example.content.adapter.recycle

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.content.*
import com.example.content.adapter.recycle.entity.HomeItemEntity
import com.example.content.adapter.recycle.entity.ItemEntity
import com.example.content.adapter.recycle.entity.MultiListItemEntity
import com.example.content.manager.ActivityMgr
import com.example.content.repository.bean.FollowReq
import com.example.content.repository.bean.UserActionReq
import com.example.content.views.RewardCoinPop
import com.example.core.extensions.showToast
import com.example.uiwidegt.view.banner.RadiusImageBanner
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem

/**
 * Created by zhudongao
 * Desc
 * Date 2020/10/4
 */
class CommonListAdapter() :
    BaseMultiItemQuickAdapter<HomeItemEntity, BaseViewHolder>() {

    private var bannerData = mutableListOf<BannerItem>()

    init {
        addItemType(HOME_BANNER, R.layout.banner_layout)
        addItemType(PHOTO_LIST_TYPE, R.layout.item_photo_layout)
        addItemType(VIDEO_TYPE, R.layout.item_video_layout)
        addItemType(FLOW_ITEM_TYPE, R.layout.hori_recycler_layout)

        // TODO: 2020/10/5 假数据
        for (i in titles.indices) {
            val bannerItem = BannerItem()
            bannerItem.setImgUrl(urls[i])
            bannerItem.setTitle(titles[i])
            bannerData.add(bannerItem)
        }
    }
    override fun convert(holder: BaseViewHolder, item: HomeItemEntity) {
        when (holder.itemViewType) {
            HOME_BANNER -> {
                val banner = holder.getView<RadiusImageBanner>(R.id.banner)
                banner.setSource(bannerData).setOnItemClickListener { view, item, position ->
                    App.appContext.showToast("点击了：${titles[position]}")
                }.startScroll()
            }
            PHOTO_LIST_TYPE -> {
                val recycler = holder.getView<RecyclerView>(R.id.recycler_view)
                val photoListAdapter = MultiListAdapter()
                val dataList = mutableListOf<MultiListItemEntity>()
                for (i in 0..5) {
                    dataList.add(MultiListItemEntity(10))
                }
                photoListAdapter.data = dataList
                recycler.adapter = photoListAdapter
                val layoutManager = GridLayoutManager(recycler.context, 3)
                recycler.layoutManager = layoutManager
                if (recycler.itemDecorationCount < 2) {
                    recycler.addItemDecoration(
                        DividerItemDecoration(
                            recycler.context,
                            DividerItemDecoration.HORIZONTAL
                        )
                    )
                    recycler.addItemDecoration(
                        DividerItemDecoration(
                            recycler.context,
                            DividerItemDecoration.VERTICAL
                        )
                    )
                }
                clickListener(holder, item)
            }
            VIDEO_TYPE -> {
                val url = "http://7xjmzj.com1.z0.glb.clouddn.com/20171026175005_JObCxCE2.mp4"
                val player = holder.getView<StandardGSYVideoPlayer>(R.id.video_show)
                player.backButton.visibility = View.GONE
                val options = GSYVideoOptionBuilder()
                options.setIsTouchWiget(true)
                    .setRotateViewAuto(false)
                    .setLockLand(false)
                    .setAutoFullWithSize(false)
                    .setShowFullAnimation(false)
                    .setNeedLockFull(true)
                    .setUrl(url)
                    .setCacheWithPlay(false)
                    .setVideoTitle("测试视频")
                    .build(player)
                player.fullscreenButton.setOnClickListener {
                    player.startWindowFullscreen(
                        player.context,
                        true,
                        true
                    )
                }
                clickListener(holder, item)
            }
            FLOW_ITEM_TYPE -> {
                val recycler = holder.getView<RecyclerView>(R.id.recycler_view)
                val adapter = RecyclerAdapter(object : BindListener<String> {
                    override fun convert(holder: BaseViewHolder, item: ItemEntity<String>) {
                        val text = holder.getView<TextView>(R.id.text)
                        text.text = item.data
                    }
                })
                for (i in FLOW_ARRAY) {
                    adapter.addData(ItemEntity(data = i))
                }
                adapter.addItemView(R.layout.oval_flow_item)
                recycler.adapter = adapter
                val manager = LinearLayoutManager(recycler.context, RecyclerView.HORIZONTAL, false)
                recycler.layoutManager = manager
            }
            else -> {

            }
        }
    }

    private fun clickListener(holder: BaseViewHolder, item: HomeItemEntity) {
        val headIcon = holder.getView<View>(R.id.head_icon)
        headIcon.setOnClickListener {
            ActivityMgr.startFragment(PERSONAL_INFO_CENTER)
        }
//        val reward = holder.getView<View>(R.id.reward)
//        reward.setOnClickListener {
//            val pop = RewardCoinPop(reward.context)
//            pop.showPopupWindow()
//        }
        //关注
        val follow = holder.getView<TextView>(R.id.follow)

        follow.setOnClickListener {
            val req = FollowReq(item.userId, if (item.isFollow) UN_ACTION else DO_ACTION)
            item.funAction?.follow(req)
            if (item.isFollow) {
                follow.text = context.getString(R.string.follow)
                follow.setTextColor(Color.parseColor("#FF4C6A"))
            } else {
                follow.text = context.getString(R.string.already_follow)
                follow.setTextColor(Color.parseColor("#999999"))
            }
            item.isFollow = !item.isFollow
        }
        //收藏点赞
        val collection = holder.getView<View>(R.id.collection)
        val collectImg = holder.getView<ImageView>(R.id.collect_img)
        val collectNum = holder.getView<TextView>(R.id.collect_num)
        collection.setOnClickListener {
            val userActionReq = UserActionReq(item.id, COLLECTION, if (item.isCollect) UN_ACTION else DO_ACTION)
            item.funAction?.userAction(userActionReq)
            if (item.isCollect) {
                collectImg.setImageResource(R.drawable.ic_collection_normal)
                collectNum.text = (--item.collectNum).toString()
            } else {
                collectImg.setImageResource(R.drawable.ic_collected)
                collectNum.text = (++item.collectNum).toString()
            }
            item.isCollect = !item.isCollect
        }
    }
}